package com.practice.question2;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-18 17:14
 */

public class MyContainer<T> {
    final private LinkedList<T> list = new LinkedList<>();
    final private int MAX = 10;
    private volatile int count=0;
    public synchronized void put(T t){
        while(list.size() == MAX){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add(t);
        count++;
        this.notifyAll();
    }
    public synchronized T get(){
        T t = null;
        while(list.size() == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        t = list.removeFirst();
        count--;
        this.notifyAll();
        return t;
    }
    public int getCount(){
        return count;
    }

}
